<?php
/**
 * Class Server
 * Desc  服务Controller
 * Create on 2021/8/26 14:31
 * Create by wangyafang
 */

namespace app\api\controller\xyb;
use app\api\model\xyb\Order;

class Server extends Base
{
    protected $noNeedLogin = ['index'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize(){
        parent::_initialize();
        $this->model = new \app\api\model\xyb\Server();
    }

    public function index()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $where['agent_id'] = array('eq',$this->request->param('agent_id'));
        $data = $this->model->getList($this->model,$where,'id,server_name,icon_image','weigh desc,id desc');
        $this->success('请求成功',$data);
    }

    public function list()
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }

        $where['agent_id'] = array('eq',$this->request->param('agent_id'));
        $data = $this->model->getAllList($where,'id,server_name','weigh desc,id desc');
        foreach ($data as $k=>$v) {
            switch ($v['server_name']) {
                case '校园跑腿':
                    $data[$k]['id'] = 1;
                    break;
                case '快递代取':
                    $data[$k]['id'] = 2;
                    break;
                case '租借服务':
                    $data[$k]['id'] = 4;
                    break;
            }
        }
        $this->success('请求成功',$data);
    }

    public function get_receive_profit($data)
    {
        if(!$this->request->isPost()) {
            $this->error('请求类型错误');
        }
        $server_name = (new Order())->getTypeTextAttr($data['type'],$data);
        $orderObj = (new Order())->get($data['id']);

        $server_info = (new \app\api\model\xyb\Server())->where([
            'agent_id'=>$data['agent_id'],
            'server_name'=>$server_name,
        ])->field('agent_profit,receive_profit,platform_profit')->find();


        $tem_platform_profit = bcmul($orderObj['total_fee'] , $server_info['platform_profit'] , 2);
        $platfrom_profit = bcmul($tem_platform_profit , 0.01 ,3);
        $platfrom_profit = substr($platfrom_profit, 0, -1);

        $tem_agent_profit = bcmul($orderObj['total_fee'] , $server_info['agent_profit'] , 2);
        $agent_profit = bcmul($tem_agent_profit , 0.01 ,3);
        $agent_profit = substr($agent_profit, 0, -1);

        $tem_receive_profit = bcmul($orderObj['total_fee'] , $server_info['receive_profit'] , 2);
        $receive_profit = bcmul($tem_receive_profit , 0.01 ,3);
        $receive_profit = substr($receive_profit, 0, -1);
        $reduce_platform = bcsub($orderObj['total_fee'],$platfrom_profit,2);
        $reduce_agent = bcsub($reduce_platform,$agent_profit,2);

        $reduce_profit = bcsub($reduce_agent,$receive_profit,2);
        if($reduce_profit == 0.01) {
            $receive_profit = $receive_profit + 0.01;
        } elseif ($reduce_profit == 0.02) {
            $receive_profit = $receive_profit + 0.01;
        } elseif ($reduce_profit == 0.03) {
            $receive_profit = $receive_profit + 0.01;
        }
        $pos = strpos($receive_profit, '.');
        if(!$pos) {
            $receive_profit = $receive_profit.'.00';
        }
        return $receive_profit;

    }
}